#include <bits/stdc++.h>
using namespace std;

#define MAX 200200

int read() {
  int x = 0;
  bool t = false;
  char ch = getchar();
  while ((ch < '0' || ch > '9') && ch != '-')
    ch = getchar();
  if (ch == '-')
    t = true, ch = getchar();
  while (ch <= '9' && ch >= '0')
    x = x * 10 + ch - 48, ch = getchar();
  return t ? -x : x;
}

int T;
int n, a[MAX << 1];
vector<int> p[MAX];
int ans;

int main() {
  T = read();
  while (T--) {
    n = read();
    ans = 0;
    for (int i = 1; i <= n; ++i)
      p[i].clear();
    for (int i = 1; i <= n + n; ++i)
      p[a[i] = read()].push_back(i);
    for (int i = 1; i <= n; ++i) {
      int p1 = p[i][0], p2 = p[i][1];
      if (p1 + 1 == p2)
        continue;
      if (p1 > 1) {
        if (a[p1 - 1] == a[p2 - 1])
          ++ans;
        if (p2 < n + n && a[p1 - 1] == a[p2 + 1])
          ++ans;
      }
      if (p1 + 1 != p2 - 1 && p1 + 2 != p2 - 1 && a[p1 + 1] == a[p2 - 1])
        ++ans;
      if (p2 < n + n && a[p1 + 1] == a[p2 + 1])
        ++ans;
    }
    printf("%d\n", ans / 2);
  }
  return 0;
}